package com.zhufei.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDao {
	private static Connection con = null;
	
	/**
	 * 登录验证函数
	 * @param username 用户名
	 * @param password 密码
	 * @return true或者false返回用户名和密码正确与否
	 */
	public static boolean login(String username, String password){
		//TODO 添加实体代码
		if(con == null){
			getConnection();
		}
		try {
			Statement statement = con.createStatement();
			String sql = "select password from user where user_id ='" +username +"'";
			ResultSet rs = statement.executeQuery(sql);
			if(!rs.next())
				return false;
			if(password.equals(rs.getString("password")))
				return true;
			else
				return false;
					
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	
	/**
	 * 获取链接对象
	 */
	private static void getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection
			( GlobalsDao.database_url, GlobalsDao.database,GlobalsDao.database_password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
